<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Card\Model;
use Think\Model;

/**
 * 用户模型
 * @author 麦当苗儿 <zuojiazi@vip.qq.com>
 */

class GreetingCardModel extends Model {

    public $sx_data = array();

    protected $_validate = array(
        //新增贺卡时验证
        array('title','','标题不能为空',1,'notequal',1),

    );

    protected $_auto = array (

        //新增贺卡时自动完成
        array('atime','time',1,'function'), // 新增贺卡时获取添加时间
        array('utime','time',1,'function'), // 新增贺卡时获取更新时间

        //登录时自动完成
        array('last_login_time','time',6,'function'), // 登录的时候对最后登录时间写入当前时间戳
        array('last_login_ip','getIp',6,'function'), // 登录的时候最后登录ip

    );

    /**
     * 查询单条贺卡信息
     */
    public function getGreetInfo($condition, $field = '*') {
        return $this->field($field)->where($condition)->find();
    }

     /**
     * 查询多条用户信息
     */
    public function getAllGreetInfo($condition, $field = '*',$order='utime desc,id desc') {
        return $this->field($field)->where($condition)->order($order)->select();
    }

    /**
     * 增加贺卡
     */
    public function addGreet(){
        if (!$this->create($this->sx_data,1)){
            $res['msg'] = $this->getError();
            $res['sta'] = -1;
            return $res;
        }else{
            $res = $this->add();
            return $res;
        }
    }

    /**
     * 保存贺卡
     */
    public function saveGreet($data){
        if(empty($data['id'])){
            return false;
        }
        $res = $this->save($data);
        return $res;

    }
    
    public function existGreetCard($where)
    {
        return M('greeting_card')->where($where)->getField('id');
    }
    
    public function getUserId($cardId)
    {
        return M('greeting_card')->where([
            'id' => $cardId
        ])->getField('user_id');
    }

}
